/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.locationbasedagent;


import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;

public class SMSComposerActivity extends Activity {

	private EditText mRecipientText;
	private EditText mMessageText;
	private Button mSendSMSButton;
	
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.messagecomposer);

	    mRecipientText = (EditText) findViewById(R.id.recipientAddressText);
	    mMessageText = (EditText) findViewById(R.id.messageText);
	    
	    mSendSMSButton = (Button) findViewById(R.id.button1);
	    mSendSMSButton.setOnClickListener(mSendMessageListener);
	    
	    Bundle bundle = getIntent().getExtras();
	    mRecipientText.setText(bundle.getString("contactinfo"));
	    
	}
	    
    private OnClickListener mSendMessageListener = new OnClickListener() {
        public void onClick(View v) {
        	String recipientAddress = mRecipientText.getText().toString();
        	String message = mMessageText.getText().toString();
        	if(recipientAddress.length() > 0 && message.length() > 0)
        	{                
        		sendSMS(recipientAddress, message); 
        	}
            else
            {
                        Toast.makeText(getBaseContext(), 
                            "Please enter both phone number and message.", 
                            Toast.LENGTH_SHORT).show();
        	}
        }
    };
    
    private void sendSMS(String phoneNumber, String message)
    {        
        PendingIntent pi = PendingIntent.getActivity(this, 0,
            new Intent(this, SMSComposerActivity.class), 0);                
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, pi, null);        
    }  
    
}